package main

import (
	"context"
	"fmt"
	"google.golang.org/grpc"
	"grpc-client/helpers"
	//点语法，表示可以直接使用
	//server "go-grpc/services"
	. "grpc-client/services"
	"log"
)

func main() {
	//conn, err := grpc.Dial(":8081", grpc.WithInsecure())
	//if err != nil {
	//	log.Fatal(err)
	//}
	//
	//defer conn.Close()
	//helloClient := server.NewGreeterClient(conn)
	//helloRespnse, err := helloClient.SayHello(context.Background(), &server.HelloRequest{Name: "zhangsan", RqId: 10})
	//helloRespnse1, err1 := helloClient.SayHelloAgain(context.Background(), &server.HelloRequest{Name: "zhangsan", RqId: 10})
	//if err != nil {
	//	log.Fatal(err)
	//}
	//
	//if err1 != nil {
	//	log.Fatal(err)
	//}
	//fmt.Println(helloRespnse)
	//fmt.Println(helloRespnse1)

	//客户端验证，简单的单方验证
	//creds, err := credentials.NewClientTLSFromFile("./tls/server.pem", "joyceyi.top")
	//if err != nil {
	//	log.Fatalf("Failed to create TLS credentials %v", err)
	//}

	creds := helpers.GetClientCert()
	// 连接服务器
	conn, err := grpc.Dial(":8085", grpc.WithTransportCredentials(creds))
	if err != nil {
		log.Fatalf("net.Connect err: %v", err)
	}
	defer conn.Close()

	// 建立grpc连接
	helloClient := NewHelloServiceClient(conn)
	helloRespnse, err := helloClient.SayHello(context.Background(), &HelloRequest{Name: "zhangsan", RqId: 10})
	helloRespnse1, err1 := helloClient.SayHelloAgain(context.Background(), &HelloRequest{Name: "zhangsan", RqId: 10})
	if err != nil {
		log.Fatal(err)
	}

	if err1 != nil {
		log.Fatal(err)
	}

	fmt.Println(helloRespnse)
	fmt.Println(helloRespnse1)

	// 建立grpc连接
	productClient := NewProductServiceClient(conn)
	productResponse, err := productClient.ProductList(context.Background(), &Query{Size: 3})
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(productResponse)

	// 建立grpc连接
	orderClient := NewOrderSerivceClient(conn)
	//t:=timestamp.Timestamp{Seconds:time.Now().Unix()}
	orderResponse, err := orderClient.NewOrder(context.Background(), &OrderMain{
		OrderId:    1,
		UserId:     1,
		OrderNo:    "no01",
		OrderMoney: 10.80,
	})
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(orderResponse)
}
